//
//  LogInViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/10.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "LogInViewController.h"
#import "SetupViewController.h"
#import "ForgivePassWordViewController.h"
#import "AppDelegate.h"
#import "MainTabbarViewController.h"

@interface LogInViewController ()

@property (nonatomic ,weak)UIImageView * backImageView;
@property (nonatomic, weak)UITextField * accountText;
@property (nonatomic, weak)UITextField * passwordText;
@property (nonatomic, weak)UIButton * loginButton;
@property (nonatomic, weak)UIButton * setupButton;
@property (nonatomic, weak)UIButton * forgiveButton;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UILabel * noAccLabel;

@end

@implementation LogInViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}


- (void)setupUI{
    
    UIImageView * backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    backImageView.image = ImageNamed(@"bg");
    backImageView.userInteractionEnabled = YES;
    
    self.backImageView = backImageView;
    
    [self.view addSubview:self.backImageView];
    
    
    UITextField * accountText = [[UITextField alloc] init];
    
    accountText.borderStyle = UITextBorderStyleNone;
    accountText.backgroundColor = [UIColor blackColor];
    //设置光标颜色
    accountText.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    accountText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入帐号" attributes:attrs];
    accountText.textColor = [UIColor whiteColor];
//    accountText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
//    //设置显示模式为永远显示(默认不显示)
//    accountText.leftViewMode = UITextFieldViewModeAlways;
    accountText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    accountText.keyboardType = UIKeyboardTypeNumberPad;
    accountText.keyboardAppearance = UIKeyboardAppearanceDark;
    accountText.textAlignment = NSTextAlignmentCenter;
    
    self.accountText = accountText;
    
    [self.view addSubview:self.accountText];
    
    
    
    UITextField * passwordText = [[UITextField alloc] init];
    
    passwordText.borderStyle = UITextBorderStyleNone;
    passwordText.secureTextEntry = YES;
    passwordText.backgroundColor = [UIColor blackColor];
    //    passwordText.delegate = self;
    //设置光标颜色
    passwordText.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    passwordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:attrs];
    passwordText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    passwordText.textColor = [UIColor whiteColor];
//    passwordText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
//    //设置显示模式为永远显示(默认不显示)
//    passwordText.leftViewMode = UITextFieldViewModeAlways;
    //    accountText.font = [UIFont systemFontOfSize:15];
        passwordText.keyboardType = UIKeyboardTypeDefault;
        passwordText.keyboardAppearance = UIKeyboardAppearanceDark;
    passwordText.textAlignment = NSTextAlignmentCenter;
    
    self.passwordText = passwordText;
    
    [self.view addSubview:self.passwordText];
    
    
    UIButton * forgiveButton = [[UIButton alloc] init];
    
    [forgiveButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    forgiveButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    //    forgiveButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [forgiveButton setTitleColor:ColorWithRGB(0xFFFFFF) forState:UIControlStateNormal];
    [forgiveButton addTarget:self action:@selector(FindPassword) forControlEvents:UIControlEventTouchUpInside];
    
    self.forgiveButton = forgiveButton;
    
    [self.view addSubview:self.forgiveButton];
    
    
    UIButton * loginButton = [[UIButton alloc] init];
    
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:17 * SCREENH6];
    [loginButton setBackgroundColor:[UIColor redColor]];
    
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(didLoginButton) forControlEvents:UIControlEventTouchUpInside];
    //  把图片设置成圆形
    loginButton.layer.cornerRadius = 25;//裁成圆角
    loginButton.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    
    self.loginButton = loginButton;
    
    [self.view addSubview:self.loginButton];
    
    
    UIButton * setupButton = [[UIButton alloc] init];
    
    [setupButton setTitle:@"立即注册" forState:UIControlStateNormal];
    setupButton.titleLabel.font = [UIFont boldSystemFontOfSize:16 * SCREENH6];
    
    [setupButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [setupButton addTarget:self action:@selector(didSetuputton) forControlEvents:UIControlEventTouchUpInside];
    
    self.setupButton = setupButton;
    
    [self.view addSubview:self.setupButton];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xFFFFFF);
    lineView.alpha = 0.4;
    
    self.lineView = lineView;
    
    [self.view addSubview:self.lineView];
    
    
    UILabel * acclabel = [UILabel new];
    
    acclabel.text = @"没有账号？";
    acclabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    acclabel.textColor = ColorWithRGB(0xFFFFFF);
    acclabel.textAlignment = NSTextAlignmentRight;
    
    self.noAccLabel = acclabel;
    
    [self.view addSubview:self.noAccLabel];
}


- (void)viewDidLayoutSubviews{
    
    [self.accountText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(54 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-54 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(278 * SCREENH6);
    }];
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.accountText);
        make.top.mas_equalTo(self.accountText.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(80 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-80 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.passwordText.mas_bottom).offset(25 * SCREENH6);
    }];
  
    [self.forgiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.loginButton);
        make.top.mas_equalTo(self.loginButton.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.passwordText);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.forgiveButton.mas_bottom).offset(95 * SCREENH6);
    }];
    
    [self.noAccLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view).offset(-35 * SCREENH6);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.setupButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.noAccLabel.mas_trailing);
        make.centerY.mas_equalTo(self.noAccLabel);
    }];
}



- (void)didLoginButton{
    
    [self.accountText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    
    if ([self.accountText.text isEqualToString:@""] || [self.passwordText.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善登录信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        SHOWHUD;
        
        NSDictionary * parametersDic = @{@"mobile":self.accountText.text,@"password":self.passwordText.text};
        
        [[NetworkTool sharedTools] POST:@"/index/Member/login" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            HIDDENHUD;
            
//            NSLog(,responseObject);

            NSLog(@"===%@",responseObject);
            
            NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            
            if ([codeString isEqualToString:@"200"]) {
                
                NSDictionary * paramDict = responseObject[@"data"];
                
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                
                NSString * token = paramDict[@"token"];
                NSString * user_id = paramDict[@"id"];
                
                [userDefaults setObject:token forKey:@"token"];
                [userDefaults setObject:user_id forKey:@"user_id"];
                
                [userDefaults synchronize];
                
                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                
                MainTabbarViewController *tabs = [[MainTabbarViewController alloc] init];
                
                appDelegate.window.rootViewController = tabs;
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            HIDDENHUD;
            
            NSLog(@"error===%@",error);
        }];
        
    }
    
}


- (void)didSetuputton{
    
    SetupViewController * setupVC = [SetupViewController new];
    
    [self.navigationController pushViewController:setupVC animated:YES];
}


- (void)FindPassword{
    
    ForgivePassWordViewController * forgiveVC = [ForgivePassWordViewController new];
    
    [self.navigationController pushViewController:forgiveVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
